package com.atguigu.daijia.map.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.map.service.MapService;
import com.atguigu.daijia.model.form.map.CalculateDrivingLineForm;
import com.atguigu.daijia.model.vo.map.DrivingLineVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zqtao
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class MapServiceImpl implements MapService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${tencent.map.key}")
    private String key; // 腾讯地图服务

    @Override
    public DrivingLineVo calculateDrivingLine(CalculateDrivingLineForm calculateDrivingLineForm) {

        String url = "https://apis.map.qq.com/ws/direction/v1/driving/?from={from}&to={to}&key={key}";

        //开始获取form to key 封装发送请求
        Map<String, String> map = new HashMap<>();

        map.put("from", calculateDrivingLineForm.getStartPointLatitude() + "," + calculateDrivingLineForm.getStartPointLongitude());
        map.put("to", calculateDrivingLineForm.getEndPointLatitude() + "," + calculateDrivingLineForm.getEndPointLongitude());
        map.put("key", key);

        //根据声明的url 发送请求
        JSONObject object = restTemplate.getForObject(url, JSONObject.class, map);

        if(object != null){

            //获取返回结果判断
            if (object.getIntValue("status") != 0) {
                throw new GuiguException(ResultCodeEnum.MAP_FAIL);
            }

            //获取返回结果 获取规划路线
            JSONObject route = object.getJSONObject("result").getJSONArray("routes").getJSONObject(0);

            DrivingLineVo drivingLineVo = new DrivingLineVo();
            //单位：千米
            drivingLineVo.setDistance(route.getBigDecimal("distance").divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_UP));
            drivingLineVo.setDuration(route.getBigDecimal("duration"));
            drivingLineVo.setPolyline(route.getJSONArray("polyline"));

            return drivingLineVo;

        }

        throw new GuiguException(ResultCodeEnum.MAP_FAIL);

    }
}
